//评论相关业务代码
const sequelize = require("../config/sequelize");
const User = require("../models/user");
const Comment = require("../models/comment");
const Post = require("../models/post");
const messageService = require("./message");
const scoreService = require("./scoreService");

//获取评论列表
exports.getList = async function (postId, size, page) {
  size = parseInt(size);
  page = parseInt(page);
  //获取父级评论
  const { rows, count } = await Comment.findAndCountAll({
    where: {
      postId,
      parentCommentId: null,
    },
    attributes: {
      exclude: ["updatedAt", "deletedAt"],
    },
    limit: size,
    offset: (page - 1) * size,
    order: [["createdAt", "DESC"]],
    include: {
      model: User,
      attributes: ["id", "username", "avatar", "college"],
      as: "user",
    },
  });
  //获取每个父级评论的子级评论
  for (let i = 0; i < rows.length; i++) {
    rows[i].dataValues.childrenComments = await Comment.findAll({
      where: {
        postId,
        parentCommentId: rows[i].id,
      },
      attributes: {
        exclude: ["updatedAt", "deletedAt"],
      },
      include: {
        model: User,
        attributes: ["id", "username", "avatar", "college"],
        as: "user",
      },
      order: [["createdAt", "ASC"]],
    });
  }
  return {
    rows,
    count,
  };
};

//新增评论
exports.add = async function (commentData) {
  let comment;
  const {
    content,
    parentCommentId,
    replyCommentId,
    flag,
    replyerId,
    replyerName,
    postId,
    userId,
    postUser,
  } = commentData;
  const post = await Post.findByPk(postId);
  if (!post) {
    throw new Error("文章已经被删除！");
  }
  //创建评论
  comment = await Comment.create({
    content,
    parentCommentId,
    replyCommentId,
    flag,
    replyerId,
    replyerName,
    postId,
    userId,
  });

  //获取评论
  comment = await Comment.findOne({
    where: {
      id: comment.id,
    },
    attributes: { exclude: ["updatedAt", "deletedAt"] },
    include: {
      model: User,
      attributes: ["id", "username", "avatar", "college"],
      as: "user",
    },
  });

  //   添加消息
  if (replyerId) {
    if (replyerId != userId) {
      await messageService.add(
        content,
        replyerId,
        userId,
        2,
        postId,
        comment.id
      );
    }
  } else {
    if (postUser != userId)
      await messageService.add(
        content,
        postUser,
        userId,
        1,
        postId,
        comment.id
      );
  }
  //评论积分加1分
  await scoreService.add(userId, 1, "创建评论");
};

//删除评论
exports.delete = async function (commentId, parentCommentId, postId, userId) {
  const post = await Post.findByPk(postId);
  if (!post) {
    throw new Error("文章已经被删除！");
  }
  parentCommentId = parentCommentId == "null" ? false : true;
  if (parentCommentId) {
    await Comment.destroy({
      where: {
        id: commentId,
      },
      individualHooks: true,
    });
    //消除消息
    await messageService.deleteComment(postId, commentId);
  } else {
    await Comment.destroy({
      where: {
        id: commentId,
      },
      individualHooks: true,
    });
    //删除消息父级评论消息
    await messageService.deleteComment(postId, commentId);
    const comments = await Comment.findAll({
      where: {
        parentCommentId: commentId,
      },
    });
    //删除父级评论下所有自己评论的消息
    let length = comments.length;
    for (let i = 0; i < length; i++) {
      await messageService.deleteComment(postId, comments[i].id);
    }

    await Comment.destroy({
      where: {
        parentCommentId: commentId,
      },
      individualHooks: true,
    });
  }
  //删除评论积分减1分
  await scoreService.add(userId, -1, "删除评论");
};

//删除某篇文章下的所有评论
exports.deleteAll = async function (postId) {
  return Comment.destroy({
    where: { postId },
  });
};
